package com.lzz.utils;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhenzhen
 * @date 2018-12-23 下午5:42
 */
public class HandleShiroUnAuthorizedExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        if (e instanceof UnauthorizedException) {
            ModelAndView mv = new ModelAndView("/403");
            mv.setStatus(HttpStatus.FORBIDDEN);
            return mv;
        }
        e.printStackTrace();
        ModelAndView mv = new ModelAndView("/403");
        mv.addObject("exception", e.getMessage());
        return mv;
    }
}
